define(
	[
		'viewer',
		'lazyload',
		'layui',
	],
	function(
	){
	var myTakePhoto_detail = {
		params:{
			url:'http://www.atphotos.cn/atphotos',
			imgStatus:'',
			loadImg:'',
			photo_count:0,
			photoId:[],
			photoImg:[],
			loadImgArr:[],
			uploadImg:'',
			imgId:'',
			upload:'',
			token:''
		},
		initPage:function(){
		    myTakePhoto_detail.lookPhoto();// 查看相册
			// 获得token
			myTakePhoto_detail.token()
			$('#zoom-window').remove();
			// 删除指定元素
			myTakePhoto_detail.delArray();
		},
		token:function(){
			myTakePhoto_detail.upImg();
		},
		// 上传图片
		upImg:function(){
			$('.update-image').change(function(){
				simple.loading();
				var form = $(this).parent()[0];
				var data = new FormData(form);     
				server.postFileData(data,myTakePhoto_detail.params.url+"/editor/uploadMultipart",function(res){
					if(res.code==0){
						simple.alert('上传成功');
						// myTakePhoto_detail.getPhotos(); //获得图片
					}else{
						simple.alert(res.message+"未上传");
					}
					simple.delLoad();
				})
			})
			/*var uploader = Qiniu.uploader({
				runtimes: 'html5,flash,html4',
		        browse_button: 'pickfiles',
		        max_file_size: '100mb',
		        chunk_size: '4mb',
		        uptoken:myTakePhoto_detail.params.token,
		        domain: 'z2',
        		auto_start: true,
        		init: {
		            'FilesAdded': function(up, files) {
		                plupload.each(files, function(file) {
		                    var progress = new FileProgress(file, 'fsUploadProgress');
		                    progress.setStatus("等待...");
		                });
		            },
		            'BeforeUpload': function(up, file) {
		                var progress = new FileProgress(file, 'fsUploadProgress');
		                var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
		                if (up.runtime === 'html5' && chunk_size) {
		                    progress.setChunkProgess(chunk_size);
		                }
		            },
		            'UploadProgress': function(up, file) {
		                var progress = new FileProgress(file, 'fsUploadProgress');
		                var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
		                progress.setProgress(file.percent + "%", file.speed, chunk_size);
		            },
		            'UploadComplete': function() {
		                $('#success').show();
		            },
		            'FileUploaded': function(up, file, info) {
		                var progress = new FileProgress(file, 'fsUploadProgress');
		                progress.setComplete(up, info);
		            },
		            'Error': function(up, err, errTip) {
		                $('table').show();
		                var progress = new FileProgress(err.file, 'fsUploadProgress');
		                progress.setError();
		                progress.setStatus(errTip);
		            }
		        }
			})*/
		},
		/*查看相册*/
		lookPhoto:function(){
			$('#myAlbum_detail span').click(function(){
				$(this).removeClass('glyphicon-menu-up');
				if($('.lookPhotoDetail').css('display') == 'none'){
					$(this).addClass('glyphicon-menu-down');
				}else{
					$(this).addClass('glyphicon-menu-up');
				}
				$('.lookPhotoDetail').slideToggle();
			})
			myTakePhoto_detail.getPhotos(); //获得图片
			myTakePhoto_detail.getAlbumMsg(); //获得相册信息
		},
		/*获得图片*/
		getPhotos:function(){
			layui.use('flow', function(){
				var flow = layui.flow;
				flow.load({
				    elem: '#content' //流加载容器
				    ,done: function(page, next){ //执行下一页的回调
				     	myTakePhoto_detail.getPhotoFlow(page,'30',next);
				    },
				});
			})	
		},
		// getPhotoFlow
		getPhotoFlow:function(page,limit,next){
			var lis = [];
			var arr;
			var random = parseInt(Math.random())
			var sendData = {
				hidden:0,
				albumId:sessionStorage.getItem('albumId'),
				page:page,
				limit:limit
			}
			sendData = JSON.stringify(sendData);
			server.getData({postData:sendData},myTakePhoto_detail.params.url+"/user/albumPhotos",function(res){
				if(res.code==0){
					if(res.data.length !=0){
						$.each(res.data,function(i){
							var random = parseInt(Math.random()*100000);
							arr = res.data[i].photoPath.split('?')[0].split('tos/')[1];
							if(res.data[i].edited =='1'){
								var img = "<li><div class='sel_del'><span class='box' photoId="+res.data[i].photoId+" photoImg='"+res.data[i].photoPath+"'  seled='select'></span>"+
						"</div><img class='lazy' data-original='"+ res.data[i].photoPath +"|imageView2/1/w/240/h/220/q/100&"+random+"'"+
								 "data-src='"+ res.data[i].photoPath +"|imageslim' width='240' height='220' alt='loading...' />"+
								 "<div class='img-url'><span>"+arr+"</span></div>"+
								 "<div class='user-sel'><a href='javascript:;' class='download-no br edit-state' photoid="+res.data[i].photoId+">已修图</a>"+
									"<a download href='"+res.data[i].photoPath+"&"+random+"' class='download br'>下载</a>"+
									"<form enctype='multipart/form-data'>替换<input type='file' name='mfile' class='update-image' /><input name='photoId' value='"+res.data[i].photoId+"' type='hidden'/></form>"+
									"</div></li>";
								$('#picture').append(img);
							}else{
								var img = "<li><div class='sel_del'><span class='box' photoId="+res.data[i].photoId+" photoImg='"+res.data[i].photoPath+"'  seled='select'></span>"+
						"</div></div><img class='lazy' data-original='"+ res.data[i].photoPath +"|imageView2/1/w/240/h/220/q/100&"+random+"'"+
								 "data-src='"+ res.data[i].photoPath +"|imageslim' width='240' height='220' alt='loading...' />"+
								 "<div class='img-url'><span>"+arr+"</span></div>"+
								 "<div class='user-sel'><a href='javascript:;' class='download-no br edit-stated' photoid="+res.data[i].photoId+">未修图</a>"+
									"<a download href='"+res.data[i].photoPath+"&"+random+"' class='download br'>下载</a>"+
									"<form enctype='multipart/form-data'>替换<input type='file' name='mfile' class='update-image' /><input name='photoId' value='"+res.data[i].photoId+"' type='hidden'/></form>"+
									"</a></div></li>";
								$('#picture').append(img);
							}
							// 设置修图状态
							myTakePhoto_detail.setImgStatus()
						})
						
						
					    $("img.lazy").lazyload({});
					    $('#picture').viewer({
							url:"data-src",
							movable:false,
							navbar:false
						});
					     // 选择图片下载本地
						myTakePhoto_detail.selImgLoad();
						// 下载选择图片
						myTakePhoto_detail.selImgBtn()
						// 删除选择的图片
						myTakePhoto_detail.selDelImgBtn();
						// 替换图片
						myTakePhoto_detail.uploadImg();
					}else{
						simple.alert('暂无图片');
					}
					next(lis.join(''), page < res.count);
					$('.layui-flow-more').remove();
				}else{
					simple.alert('获得图片失败');
				}
				simple.delLoad();
			})
		},
		// 替换图片
		uploadImg:function(){
			$('.update-image').change(function(){
				var form = $(this).parent()[0];
				 var data = new FormData(form);     
   				console.log(data)

   				server.postFileData(data,myTakePhoto_detail.params.url+"/editor/upload",function(res){
   					if(res.code==0){
						simple.alert('替换成功！');
						// myTakePhoto_detail.getPhotos(); //获得图片
					}else{
						simple.alert('失败');
					}

   				})
				
			})
		},
		// 获得相册信息
		getAlbumMsg:function(){
			var sendData = {
				userId:JSON.parse(sessionStorage.getItem('user')).userId,
				albumId:sessionStorage.getItem('albumId')
			}
			sendData = JSON.stringify(sendData);
			server.getData({postData:sendData},myTakePhoto_detail.params.url+"/user/userAlbumInfo",function(res){
				if(res.code==0){
					var p = "<p>相册名称：<span>"+res.data.album_name+"</span></p><p>拍摄地址：<span>"+res.data.address+"</span></p><p>拍摄时间："+
					"<span>"+res.data.date+"</span></p><p>相册所属：<span>"+res.data.userName+"</span></p><p>关联信息：<span>"+res.data.photographers+"</span></p>";
					$('.lookPhotoDetail').append(p);
				}
			})
			myTakePhoto_detail.back(); // 返回
		},
		// 返回按钮
		back:function(){
			$('#back').click(function(){
				$.get('/admin/retoucherBackstage/albumPhoto/albumPhoto.html',function(data){
					$('#content').html(data);
					myTakePhoto_detail.params.photoId = [];
					myTakePhoto_detail.params.photoImg = [];
					myTakePhoto_detail.params.photo_count = 0;
				})
			})
		},
		/*选择图片下载本地*/
		selImgLoad:function(){
			$('#picture li .sel_del .box').unbind('click').click(function(event){
				var oEvent = event || window.event;
                oEvent.cancelBubble = true; 
                oEvent.stopPropagation();
				if($(this).attr('seled')=='select'){
					var i = "<i><img src='/admin/dist/images/ok.png' alt='' /></i>";
					$(this).append(i);
					$(this).attr('seled','selected');
					$(this).parent().css({
						'display':'block'
					})
					myTakePhoto_detail.params.photo_count++;
					// 删除相册
					$('.user-btn i.del-count').html(myTakePhoto_detail.params.photo_count);
					// 下载相册
					$('.user-btn i.download-count').html(myTakePhoto_detail.params.photo_count);
					if(myTakePhoto_detail.params.photoId.indexOf($(this).attr('photoid')) == -1){
						myTakePhoto_detail.params.photoId.push($(this).attr('photoid'))
					}
					if(myTakePhoto_detail.params.photoImg.indexOf($(this).attr('photoimg')) == -1){
						myTakePhoto_detail.params.photoImg.push($(this).attr('photoimg'))
					}

				}else{
					$(this).find('i').remove();
					$(this).attr('seled','select');
					$(this).parent().removeAttr('style');
					// 删除相册   // 下载相册
					myTakePhoto_detail.params.photo_count--;

					// 删除图片
					$('.user-btn i.del-count').html(myTakePhoto_detail.params.photo_count);
					// 下载图片
					$('.user-btn i.download-count').html(myTakePhoto_detail.params.photo_count);
					myTakePhoto_detail.params.photoId.remove($(this).attr('photoid'));
					myTakePhoto_detail.params.photoImg.remove($(this).attr('photoimg'));
				}
			});
		},
		// 设置图片状态
		setImgStatus:function(){
			$('#picture li .user-sel a').unbind('click').click(function(){
				if($(this).index()==0){
					if($(this).attr('class').split(' ')[2]=='edit-state'){
						$(this).removeClass('edit-state').addClass('edit-stated').html('未修图');
						var sendData = {
							photoId:$(this).attr('photoid'),
							status:0,
						}
						sendData = JSON.stringify(sendData);
						server.postData(sendData,myTakePhoto_detail.params.url+"/user/setEdited",function(res){
							
						})
					}else{
						$(this).removeClass('edit-stated').addClass('edit-state').html('已修图');
						var sendData = {
							photoId:$(this).attr('photoid'),
							status:1,
						}
						sendData = JSON.stringify(sendData);
						server.postData(sendData,myTakePhoto_detail.params.url+"/user/setEdited",function(res){
							
						})
					}
				}			
			})
		},
		// 删除数组指定元素
		delArray:function(){
			Array.prototype.remove = function(val) {
				var index = this.indexOf(val);
				if (index > -1) {
					this.splice(index, 1);
				}
			};
		},
		// 选择下载的图片
		selImgBtn:function(){
			$('.user-btn .download').unbind('click').click(function(){
				for(var a = 0; a < myTakePhoto_detail.params.photoId.length ; a++){
					var arr = {
						photoId:myTakePhoto_detail.params.photoId[a],
					}
					myTakePhoto_detail.params.loadImgArr.push(arr);
				}
				var sendData = JSON.stringify(myTakePhoto_detail.params.loadImgArr);
				sendData = encodeURI(sendData);
				window.location.href = myTakePhoto_detail.params.url+"/editor/download?postData="+sendData;
				
			})
		},
		// 选择删除的图片
		selDelImgBtn:function(){
			$('.user-btn .del-photo').unbind('click').click(function(){
				var sendData = {
					id:myTakePhoto_detail.params.photoId,
					key:myTakePhoto_detail.params.photoImg,
				}
				sendData = JSON.stringify(sendData);
				server.postData(sendData,myTakePhoto_detail.params.url+"/user/deletePhotos",function(res){
					/*if(res.code==0){
						simple.alert('删除成功！');
						window.location.reload();
					}*/
					simple.alert('删除成功！');
					window.location.reload();
				})
			})
		},
	}
	return myTakePhoto_detail;
})
